﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SObject
{
    public partial class Object
    {
        /// <summary>
        /// Each object must have a name
        /// </summary>
        /// <param name="Name"></param>
        public Object(string Name)
        {
            if (false == NameUtility.IsNameValid(Name))
            {
                StringBuilder stringBuilder = new StringBuilder();
                stringBuilder.AppendFormat(@"The ""{0}"" is not a valid name.", Name);
                throw (new SObject.Exception(stringBuilder.ToString()));
            }

            this.root = this;
            this.parent = null;
            this.children = new Dictionary<string, Object>();
            this.Name = Name;
        }

        public string Name
        {
            get { return (this.name); }
            set { SetName(value); }
        }

        public string CompleteName
        {
            get { return (this.completeName); }
        }

        public Object Root
        {
            get { return (this.root); }
            private set { this.root = value; }
        }

        public Object Parent
        {
            get { return (this.parent); }
            private set { this.parent = value; }
        }

        public Dictionary<string,Object> Children
        {
            get { return (this.children); }
        }

        /// <summary>
        /// Changes the name of the object.
        /// Since the complete name is dependent on the parents' names, this affects all children.
        /// Updating all children in the sub-tree is expensive.  This is a trade off between updating
        ///   upon every access, assuming name changes are infrequent.
        /// </summary>
        /// <param name="Name"></param>
        private void SetName(string Name)
        {
            this.name = Name;
            UpdateCompleteName();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <note>
        /// It is difficult to write unit test for this method.  The "inputs" in this are members
        /// this.Parent.CompleteName and this.Name.  In order to change these, to setup test cases, test 
        /// method must go through public access to these, which ultimately ends of calling this method in
        /// some way.
        /// </note>
        private void UpdateCompleteName()
        {
            if (null != this.Parent)
            {
                this.completeName = NameUtility.ConstructCompleteName(this.Parent.CompleteName, this.Name);
            }
            else
            {
                this.completeName = this.Name;
            }

            foreach (KeyValuePair<string, Object> childKvp in this.children)
            {
                Object child = childKvp.Value;
                child.UpdateCompleteName();
            }
        }

        private string name;

        /// <summary>
        /// Hierarchical name
        ///     Example: if this object is 3rd in the hierarchical in which "Name" is C,
        ///     "CompleteName" is A.B.C, in which B is parent of this object, and A is parent of B.
        ///     Where "." is the hierarchical separator, which may or may not be the character ".".
        /// </summary>
        private string completeName;
        
        /// <summary>
        /// Provides easy access to "root" for each object
        /// </summary>
        private Object root;

        private Object parent;

        private Dictionary<string, Object> children;
    }
}
